<template>
	<view class="subscription">
		<div class="sub-title">收款明细</div>
		<div class="sub-cards">
			<div class="sub-card" v-for="item in listData" :key="item.HNYY_SALE_GATHERING_BILL_ID">
				<u-row customStyle="margin-bottom: 20rpx">
					<u-col span="12">
						<div class="content">
							<div class="label">收款客户:</div>
							<div class="value">{{item.CLIENT_NAME}}</div>
						</div>
					</u-col>
				</u-row>
				<u-row customStyle="margin-bottom: 20rpx">
					<u-col span="6">
						<div class="content">
							<div class="label">收款日期:</div>
							<div class="value">{{item.IMP_DATE | filterTimes}}</div>
						</div>
					</u-col>
					<u-col span="6">
						<div class="content flex-end">
							<div class="label">收款金额:</div>
							<div class="value">
								￥{{item.AMOUNT}}
							</div>
						</div>
					</u-col>
				</u-row>
				<u-row>
					<u-col span="12">
						<div class="content">
							<div class="label">备注:</div>
							<div class="value">{{item.MEMO || '无'}}</div>
						</div>
					</u-col>
				</u-row>
			</div>
		</div>
		<uni-pagination @change="handleChangePage" v-if="page.total" title="标题文字" show-icon="true" :total="page.total"
			:current="page.current" :pageSize="page.size"></uni-pagination>
		<view class="fixed-search" @click="handleRightClick">
			<u-icon name="search" color="#fff" size="28"></u-icon>
		</view>
		<u-popup :show="show" closeable @close="handleClose" mode="right">
			<view class="popup-card">
				<u--form labelPosition="top" :model="model1" :rules="rules" ref="form1">
					<u-form-item label="客户编码" prop="userInfo.ClientCode" borderBottom ref="ClientCode"
						@click="showKh = true;hideKeyboard()">
						<u--input v-model="model1.userInfo.ClientCode" placeholder="请点击选择..." disabled
							disabledColor="#ffffff" border="none">
						</u--input>
					</u-form-item>
					<u-form-item label="客户ID" prop="userInfo.ClientId" borderBottom ref="ClientId"
						@click="showKh = true;hideKeyboard()">
						<u--input v-model="model1.userInfo.ClientId" placeholder="请点击选择..." disabled
							disabledColor="#ffffff" border="none"></u--input>
					</u-form-item>
					<u-form-item label="客户名称" prop="userInfo.ClientName" borderBottom ref="ClientId"
						@click="showKh = true;hideKeyboard()">
						<u--input v-model="model1.userInfo.ClientName" placeholder="请点击选择..." disabled
							disabledColor="#ffffff" border="none"></u--input>
					</u-form-item>
					<u-form-item label="认款状态" prop="userInfo.op_flag_name" borderBottom ref="op_flag_name"
						@click="showDepartment = true; hideKeyboard()">
						<u--input v-model="model1.userInfo.op_flag_name" placeholder="请点击选择..." disabled
							disabledColor="#ffffff" border="none">
						</u--input>
					</u-form-item>
					<u-form-item label="导入开始日期" prop="userInfo.StartDate" borderBottom ref="item1"
						@click="showStartDate = true; hideKeyboard()">
						<u--input v-model="model1.userInfo.StartDate" placeholder="请点击选择..." disabled
							disabledColor="#ffffff" border="none">
						</u--input>
					</u-form-item>
					<u-form-item label="导入结束日期" prop="userInfo.EndDate" borderBottom ref="item1"
						@click="showDate = true; hideKeyboard()">
						<u--input v-model="model1.userInfo.EndDate" placeholder="请点击选择..." disabled
							disabledColor="#ffffff" border="none">
						</u--input>
					</u-form-item>
				</u--form>
				<view class="btn-group">
					<u-button :plain="true" text="重置" size="small" :hairline="true" @click="handleReset"></u-button>
					<u-button type="primary" text="查询" size="small" :disabled="loading" @click="handleDownload">
					</u-button>
				</view>
			</view>
			<u-picker title="认款状态" :show="showDepartment" ref="uPicker" :columns="saleList" keyName="label"
				@close="handleCancelDepartment" @cancel="handleCancelDepartment" @confirm="handleConfirmDepartment">
			</u-picker>
			<u-datetime-picker title="导入开始日期" :show="showStartDate" :value="Startdate" mode="date" closeOnClickOverlay
				@cancel="handleCancelDates" ref="datetimePicker" @close="handleCancelDates" @confirm="handleConfirms">
			</u-datetime-picker>
			<u-datetime-picker title="导入结束日期" :show="showDate" :value="date" mode="date" closeOnClickOverlay
				@cancel="handleCancelDate" ref="datetimePicker" @close="handleCancelDate" @confirm="handleConfirm">
			</u-datetime-picker>
			<u-popup :show="showKh" mode="center" :round="10" closeable @close="handleCloseKh">
				<view class="kh-card">
					<div class="search">
						<u-search placeholder="请输入..." :showAction="false" v-model="keyword" @search="handleSearchKh">
						</u-search>
					</div>
					<div class="cards">
						<div class="card" v-for="item in khList" :key="item.COM_PARTY_ID" @click="handleClickKh(item)">
							<div>客户ID：{{item.COM_PARTY_ID}}</div>
							<div>客户名称：{{item.PARTY_NAME}}</div>
							<div>客户代码：{{item.PARTY_OPCODE}}</div>
							<div>拼音：{{item.SPELL}}</div>
						</div>
					</div>
				</view>
			</u-popup>
		</u-popup>
	</view>
</template>

<script>
	import store from "@/store";
	import _ from "lodash"
	import {
		searchKh,
		subQuery
	} from "@/api";
	import dayjs from "dayjs"
	export default {
		filters: {
			filterTimes(val) {
				if (val) {
					return dayjs(val).format("YYYY/MM/DD")
				} else {
					return val
				}
			},
		},
		data() {
			return {
				showStartDate: false,
				showDate: false,
				loading: false,
				khList: [],
				keyword: '',
				showKh: false,
				Startdate: dayjs().startOf('month').format('YYYY-MM-DD'),
				date: dayjs().format('YYYY-MM-DD'),
				saleList: [
					[{
							label: "未认款",
							value: "NEW"
						},
						{
							label: "部分认款",
							value: "ACCEPT"
						},
						{
							label: "认款完成",
							value: "FINISH"
						},
					]
				],
				showDepartment: false,
				page: {
					current: 1,
					size: 50,
					total: 0
				},
				listData: [],
				show: false,
				model1: {
					userInfo: {
						ClientId: "",
						ClientName: "",
						ClientCode: "",
						op_flag: "",
						op_flag_name: "",
						StartDate: dayjs().startOf('month').format('YYYY-MM-DD'),
						EndDate: dayjs().format('YYYY-MM-DD')
					}
				},
				rules: {
					'userInfo.ClientId': {
						type: 'string',
						required: true,
						message: '请选择客户',
						trigger: ['blur', 'change']
					},
				},
			}
		},
		onLoad() {
				this.handleDownload()
		},
		methods: {
			handleConfirms(e) {
				this.showStartDate = false
				this.model1.userInfo.StartDate = dayjs(e.value).format('YYYY-MM-DD')
			},
			handleConfirm(e) {
				this.showDate = false
				this.model1.userInfo.EndDate = dayjs(e.value).format('YYYY-MM-DD')
			},
			handleCancelDate() {
				this.showDate = false
			},
			async handleDownload() {
				this.loading = true
				uni.showLoading({
					title: "查询中",
					mask: true
				})
				try {
					const userInfo = uni.getStorageSync('userinfo')
					const model = {
						userid: userInfo.USER_ID || '',
						ls_start_date: this.model1.userInfo.StartDate || dayjs().startOf('month').format('YYYY-MM-DD'),
						ls_end_date: this.model1.userInfo.EndDate || dayjs().format('YYYY-MM-DD'),
						ls_op_flag: this.model1.userInfo.op_flag || 'NEW',
						page_size: this.page.size || 50,
						page_index: this.page.current || 1,
						client_id: this.model1.userInfo.ClientId || '',
						client_code: this.model1.userInfo.ClientCode || '',
						client_name: this.model1.userInfo.ClientName || '',
					}
					console.log(model);
					const res = await subQuery(model)
					if (!res.Data) {
						uni.showToast({
							title: res.Msg,
							icon: "error"
						})
					} else {
						const {
							Data
						} = res
						const {
							total,
							List
						} = Data
						this.listData = List
						this.show = false
						this.page.total = total || 0
					}
				} catch (e) {
					//TODO handle the exception
				} finally {
					this.loading = false
					uni.hideLoading()
				}
			},
			handleReset() {
				this.model1.userInfo = {
					ClientId: "",
					ClientName: "",
					ClientCode: "",
					op_flag: "",
					op_flag_name: "",
					StartDate: dayjs().startOf('month').format('YYYY-MM-DD'),
					EndDate: dayjs().format('YYYY-MM-DD')
				}
			},
			// 搜索客户
			async handleSearchKh(val) {
				if (!_.trim(val)) {
					return uni.showToast({
						title: "请输入搜索关键词"
					})
				}
				uni.showLoading({
					title: "搜索中",
					mask: true
				})
				try {
					const userinfo = uni.getStorageSync('userinfo')
					const res = await searchKh(val, userinfo.USER_ID)
					if (res.State == 1) {
						this.khList = res.Data
					} else {
						uni.showToast({
							title: res.Msg,
							icon: "error"
						})
					}
				} catch (e) {
					//TODO handle the exception
				} finally {
					uni.hideLoading()
				}
			},
			handleClickKh(row) {
				this.showKh = false
				this.model1.userInfo.ClientCode = row.PARTY_OPCODE
				this.model1.userInfo.ClientId = row.COM_PARTY_ID
				this.model1.userInfo.ClientName = row.PARTY_NAME
			},
			handleCloseKh() {
				this.showKh = false
			},
			handleConfirmDepartment(val) {
				const row = val.value[0]
				this.model1.userInfo.op_flag = row.value
				this.model1.userInfo.op_flag_name = row.label
				this.handleCancelDepartment()
			},
			// changeHandler(e){
			// 	const {
			// 		columnIndex,
			// 		index,
			// 		// 微信小程序无法将picker实例传出来，只能通过ref操作
			// 		picker = this.$refs.uPicker
			// 	} = e
			// 	const row = this.saleList[columnIndex][index]
			// 	this.model1.userInfo.op_flag = row.value
			// 	this.model1.userInfo.op_flag_name = row.label
			// },
			handleCancelDates() {
				this.showStartDate = false
			},
			handleClose() {
				this.show = false
			},
			handleCancelDepartment() {
				this.showDepartment = false
			},
			handleChangePage(e) {
				const {
					current
				} = e
				this.page.current = current
				this.handleDownload()
			},
			handleRightClick() {
				this.show = true
			},
			hideKeyboard() {
				uni.hideKeyboard()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.subscription {
		position: relative;
		padding: 32rpx;
		padding-bottom: 100rpx;

		.sub-title {
			font-size: 36rpx;
			font-weight: bold;
			margin-bottom: 30rpx;
			margin-left: 16rpx;
			position: relative;

			&::before {
				position: absolute;
				content: '';
				width: 6rpx;
				height: 80%;
				background-color: $u-primary;
				top: 8rpx;
				left: -16rpx;
			}
		}

		.sub-cards {
			.sub-card {
				box-shadow: 0 4rpx 24rpx rgba(0, 0, 0, .1);
				background-color: #fff;
				border-radius: 8rpx;
				padding: 32rpx;
				margin-bottom: 30rpx;

				.content {
					display: flex;
					font-size: 28rpx;

					.label {
						width: 140rpx;
					}

					.value {
						flex: 1;
						font-size: 24rpx;
					}
				}
			}
		}
	}

	.flex-end {
		text-align: right;
	}

	.fixed-search {
		width: 80rpx;
		height: 80rpx;
		position: fixed;
		right: 8rpx;
		top: 50%;
		background-color: $u-primary;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 8rpx;
	}

	.popup-card {
		padding: 50rpx 32rpx;
		width: calc(100vw - 64rpx);
		height: calc(100vh - 100rpx);
		overflow-y: scroll;
	}

	.btn-group {
		margin-top: 40rpx;
		display: grid;
		grid-template-columns: repeat(2, 1fr);
		grid-gap: 24rpx;
	}

	/deep/.u-form-item__body__left {
		width: 200rpx !important;
	}

	/deep/.u-form-item__body__left__content__label {
		font-size: 28rpx !important;
	}

	.u-input__content__field-wrapper input {
		font-size: 24rpx !important;
	}

	.kh-card {
		padding: 20rpx 32rpx;
		width: calc(100vw - 64rpx);
		height: calc(100vh - 100rpx);
		overflow-y: scroll;

		.search {
			margin-top: 80rpx;
			margin-bottom: 50rpx;
		}

		.cards {
			.card {
				box-shadow: 0 4rpx 24rpx rgba(0, 0, 0, .1);
				padding: 24rpx;
				border-radius: 8rpx;
				margin-bottom: 32rpx;

				div {
					font-size: 28rpx;
					line-height: 48rpx;
					color: #333333;
				}
			}
		}
	}
	.tool-page {
		.remark {
			margin-bottom: 20rpx;
			display: flex;
			align-items: center;
	
			span {
				font-size: 28rpx;
			}
	
			/deep/.u-input {
				display: inline !important;
				background-color: #fff !important;
				max-width: 600rpx !important;
				padding: 6rpx 9rpx !important;
			}
		}
	
		.page {
			display: flex;
			align-items: center;
			margin-bottom: 20rpx;
	
			span {
				font-size: 28rpx;
			}
	
			/deep/.u-input {
				display: inline !important;
				background-color: #fff !important;
				max-width: 100rpx !important;
				padding: 6rpx 9rpx !important;
			}
	
			.search-btn {
				margin-left: 10rpx;
				font-size: 28rpx;
				background-color: $u-primary;
				padding: 12rpx 18rpx !important;
				color: #fff;
				border-radius: 8rpx;
				cursor: pointer;
			}
		}
	
		.remark {}
	}
</style>
